<?php
class GoodsControl extends Control{
	//商品默认显示的首页
	function index(){
		 $Model=K("Goods");
		 //实例化出一个分页类
		$page=new Page($Model->count(),7);
                   $data=$Model->order('gid desc')->limit($page->limit())->all();
		$this->assign("page",$page->show());
		$this->assign("data",$data);
		$this->display();
	}
	//添加商品
	function add(){
	   if(IS_POST && !isset($_POST['gtid_cid'])){
	   	$Model=K("Goods");
	   	if($Model->addGoods()){
	   		$this->success("操作成功",U("index"));
	   	}else{
	   		$this->error($Model->error);
	   	}
	   }else{
	   	//获得商品类型的id
	   	$gtid_cid=$_POST['gtid_cid'];
	   	$arr=explode("|",$gtid_cid);
	   	// p($arr);
	   	//调取属性值所需的模板
	   	$attrFormModel=k("attrForm");
                   $attrData=$attrFormModel->attrForm($arr[0]);
                  //获得商品规格表单处理
                   $goodsSpec=K("GoodsSpec");
                   $GoodSpecData=$goodsSpec->goodsSpec($arr[0]);
                   $categoryData=cache("category");
	   	//查找品牌
		 $brand=cache("brand");
	   	//查找栏目
	   	$this->assign("GoodSpecData",$GoodSpecData);
	   	$this->assign("arr",$arr);
	   	$this->assign("attrData",$attrData);
	   	$this->assign("brand",$brand);
	   	$this->assign("data",$categoryData);
	   	$this->display();
	   }
	}
	//选择商品添加的栏目
	function category(){
		$category=cache("category");
		$this->assign("category",$category);
		$this->display();
	}
	//编辑商品
	function edit(){
		if(IS_POST){
                        $Model=K("Goods");
                        if($Model->editGoods()){
                        	// die;
                        	$this->success("操作成功..");
                        }else{
                        	$this->error($Model->error);
                        }
		}else{
		$gid=Q("gid",0,"intval");//要编辑的商品
		 //查找商品
		$data=M("goods")->where(array("gid"=>$gid))->find();
		 //查找栏目
		$category=cache("category");
                      //查找品牌
		$brand=cache("brand");
		  // 内容图片的获得
		$pic=M("goods_pic")->where(array("goods_gid"=>$gid))->all();
		//获取商品的普通属性
		$Model=K("GoodsAttrEdit");
		$GoodsAttrData=$Model->GoodsAttr($gid);
		//获取规格属性和库存的数据
		$Model=K("GoodSpecEdit");
		$GoodSpecData=$Model->GoodSpec($gid);
		// p($GoodSpecData);die;
		$this->assign("GoodSpecData",$GoodSpecData);
		$this->assign("GoodsAttrData",$GoodsAttrData);
		$this->assign("pic",$pic);
		$this->assign('brand',$brand);
		$this->assign("category",$category);
		$this->assign("data",$data);
                   $this->display();
		}
	}
	//删除商品
	function del(){
		$Model=K("Goods");
		if($Model->delGoods()){
		        $this->success("操作成功。。",U("index"));
		}else{
			$this->error($Model->error);
		}
	}
        //删除商品内容的图片
       function delContentPic(){
       		$model=K("Goods");
       	if($model->delContentPic()){
                    echo "1";
       	}else{
                    echo "0";
       	}
       	exit;
       }
}
